package psn.hui.bus.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import psn.hui.bus.domain.Car;
import psn.hui.bus.service.CarService;
import psn.hui.bus.vo.CarVo;
import psn.hui.sys.constant.SysConstant;
import psn.hui.sys.utils.AppFileUtils;
import psn.hui.sys.utils.DataGridView;
import psn.hui.sys.utils.JsonResult;

/**
 * 	车辆管理控制器
 * @author hui
 *
 */
@RestController
@RequestMapping("car")
public class CarController {
	
	@Autowired
	private CarService carService;
	
	/**
	 * 	插入车辆数据
	 * 
	 * @param car
	 * @return
	 */
	@RequestMapping("addCar")
	public JsonResult addCar(Car car) {
		try {
			if(carService.addCar(car) > 0) {
				return SysConstant.ADD_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.ADD_ERROR;
	}

	/**
	 * 	根据主键(carnumber)删除车辆
	 * 
	 * @param carnumber
	 * @return
	 */
	@RequestMapping("deleteCar")
	public JsonResult deleteCar(String carnumber) {
		try {
			if(carService.deleteByCarnumber(carnumber) > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}

	/**
	 * 根据主键(carnumber)更新车辆数据，动态sql(可选字段)
	 * 
	 * @param car
	 * @return
	 */
	@RequestMapping("updateCar")
	public JsonResult updateCar(Car car) {
		try {
			if(carService.updateByCarnumber(car) > 0) {
				return SysConstant.UPDATE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.UPDATE_ERROR;
	}

	/**
	 * 根据主键(carnumber)查询车辆
	 * 
	 * @param carnumber
	 * @return
	 */
	@RequestMapping("loadCarByCarnumber")
	public Car loadCarByCarnumber(String carnumber) {
		try {
			return carService.selectByCarnumber(carnumber);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Car();
	}

	/**
	 * 根据可选字段查询车辆
	 * 
	 * @param carVo
	 * @return
	 */
	@RequestMapping("carList")
	public DataGridView carList(CarVo carVo) {
		try {
			return carService.queryAllCar(carVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new DataGridView();
	}
	
	/**
	 * 批量删除车辆数据
	 * @param carVo
	 * @return
	 */
	@RequestMapping("batchDeleteCar")
	public JsonResult batchDeleteCar(CarVo carVo) {
		try {
			if(carService.batchDeleteCar(carVo.getCarnumbers()) > 0) {
				return SysConstant.DELETE_SUCCESS;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SysConstant.DELETE_ERROR;
	}
}
